package com.android.boncen.commonFrame.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.adapter.AnswerAdapter;
import com.android.boncen.commonFrame.bean.CommentBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


public class AnswerListActivity extends FragmentActivity {

    @BindView(R.id.lvList)
    ListView lvList;

    AnswerAdapter answerAdapter;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.user)
    ImageView user;

    @BindView(R.id.ib_back)
    ImageButton ibBack;

    public static void startUI(Context context, String consultationId) {

        Intent intent = new Intent (context, AnswerListActivity.class);
        intent.putExtra ("data", consultationId);
        context.startActivity (intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_answer);
        ButterKnife.bind (this);
        tvTitle.setText ("回复记录");
        ibBack.setVisibility (View.VISIBLE);
        answerAdapter = new AnswerAdapter (this);
        lvList.setAdapter (answerAdapter);
        String consultationId = getIntent ().getStringExtra ("data");
        getConsultationBean (consultationId);
    }

    public void getConsultationBean(String consultationId) {

        BmobQuery<CommentBean> query = new BmobQuery<> ();
        query.addWhereEqualTo ("consultationId", consultationId);
        query.include ("user");
        query.order ("-updatedAt");

        query.findObjects (new FindListener<CommentBean> () {

            @Override
            public void done(List<CommentBean> object, BmobException e) {

                if (e == null) {
                    answerAdapter.setData (object);
                }
            }

        });
    }

    @OnClick(R.id.ib_back)
    public void onClick() {

        finish ();
    }
}
